from py2neo import *
from py2neo import NodeMatcher, RelationshipMatch
sqlByIndex2 = """select  *   from   temp_ods_ytb_view_data_indicator2_da where  templetname  = '%s'  and   indicatorname = '%s' and calcName  = '%s' and  timecode = '202203'"""
sqlByIndexArea1 = """select  *   from   temp_ods_ytb_view_data_indicator2_da where  templetname  = '%s'  and  indicatorname = '%s' and calcName  = '%s' and  areaname ='%s'  and  timecode = '202203'"""
sqlByIndexArea2 = """select  *   from   temp_ods_ytb_view_data_indicator2_da where  templetname  = '%s'  and  indicatorname = '%s' and calcName  = '%s' and  areaname ='%s'  and  timecode = '202203'"""



graph = Graph("http://101.35.200.120:7474/",auth=("neo4j", "xht18668071597"))

graph.delete_all()  # 需要清空数据库

# 创建结点
# 语义类型person,结点名字张三,也是它的属性
node1 = Node('person', name = '张三')
node2 = Node('address', name = '北京市海淀区')
node3 = Node('person', name = '李四')

# 给node1添加属性
node1['age'] = 22

# 给node3添加一个属性 sex
node3['sex'] = '男'

# 结点实例化
graph.create(node1)
graph.create(node2)
graph.create(node3)

# 创建关系
address = Relationship(node1, '地址', node2)
classmates = Relationship(node1, '同学', node3)
address1 = Relationship(node3, '地址', node2)

# 关系实例化
graph.create(address)
graph.create(address1)
graph.create(classmates)




# 按条件查询节点
nodes = NodeMatcher(graph)
zhao_node = nodes.match("person", name="22").all()

print(zhao_node)

# 按条件查询关系
zhao_relation = RelationshipMatch(graph, nodes=zhao_node).all()

print(zhao_relation)

